package br.com.cinesystem.persistence;

import br.com.cinesystem.entity.Usuario;
import br.com.cinesystem.entity.Venda;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class VendaDao extends Dao {

    public VendaDao() {
    }
    
    public Venda salvar(Venda v)throws Exception{

        abrirConexao();

        SimpleDateFormat dformata = new SimpleDateFormat("yyyy/MM/dd");
        String data = dformata.format(v.getData());

        SimpleDateFormat hformatada = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String hora = hformatada.format(v.getHora());

        stmt = con.prepareStatement("INSERT INTO venda VALUES (NULL,?,?,?,'CA')");
        stmt.setInt(1, v.getUsuario().getIdUsuario());
        stmt.setString(2, data);
        stmt.setString(3, hora);

        stmt.execute();

        stmt = con.prepareStatement("select LAST_INSERT_ID() as id");

        rs = stmt.executeQuery();

        Venda venda = null;

        if(rs.next()){
            venda = new Venda();
            venda.setIdVenda(rs.getInt("id"));
        }

        fecharConexao();
        return venda;
    }

    public Venda verificarCarrinho(Venda v)throws Exception{

        abrirConexao();

        stmt = con.prepareStatement("SELECT * FROM venda WHERE id_Usuario=? and situacao='CA'");
        stmt.setInt(1, v.getUsuario().getIdUsuario());

        rs = stmt.executeQuery();

        Venda venda = null;

        if(rs.next()){
            venda = new Venda();
            venda.setIdVenda(rs.getInt("idVenda"));
        }

        fecharConexao();

        return venda;
    }
    public Boolean verificarPeriodo(Date d1, Date d2)throws Exception{
        abrirConexao();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

        stmt = con.prepareStatement("SELECT * FROM venda WHERE situacao='CC' and data>=? and data<=?");
        stmt.setString(1, sdf.format(d1));
        stmt.setString(2, sdf.format(d2));

        rs = stmt.executeQuery();

        fecharConexao();
        if(rs.next()){
            return true;
        } else {
            return false;
        }
    }
    public List<Venda> buscarVendas(Usuario u)throws Exception{
        abrirConexao();

        stmt = con.prepareStatement("SELECT * FROM venda WHERE situacao='CC' and id_Usuario=?");
        stmt.setInt(1, u.getIdUsuario());

        rs=stmt.executeQuery();

        List<Venda> lista = new ArrayList<Venda>();

        while(rs.next()){
            SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");

            Venda venda = new Venda();
            venda.setUsuario(new Usuario());
            venda.getUsuario().setIdUsuario(rs.getInt("id_Usuario"));
            venda.setIdVenda(rs.getInt("idVenda"));
            venda.setData((Date) sdf1.parse(sdf1.format(rs.getDate("data"))));
            venda.setHora((Date) sdf2.parse(sdf2.format(rs.getTime("hora"))));
            venda.setSituacao(rs.getString("situacao"));

            lista.add(venda);
        }

        fecharConexao();
        return lista;
    }
}
